package com.chengxiaoxiao.ai_demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chengxiaoxiao.ai_demo.api.KnowledgeFileControllerApi;
import com.chengxiaoxiao.ai_demo.entity.KnowledgeFile;
import com.chengxiaoxiao.ai_demo.service.KnowledgeFileService;
import com.chengxiaoxiao.ai_demo.vo.PageResult;
import com.chengxiaoxiao.ai_demo.vo.Result;
import com.chengxiaoxiao.ai_demo.vo.query.KnowledgeFileQuery;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 知识库文件 接口控制器
 *
 * @Description:
 * @Author: Cheng Xiaoxiao
 * @Date: 2025-03-14 16:50:21
 */
@RestController
@RequestMapping("/knowledge-file")
public class KnowledgeFileController implements KnowledgeFileControllerApi {
    @Resource
    private KnowledgeFileService knowledgeFileService;


    /**
     * 获取条件查询信息
     *
     * @param knowledgeFileQuery 查询条件
     * @return 查询封装
     */
    private QueryWrapper<KnowledgeFile> getKnowledgeInfoQueryWrapper(KnowledgeFileQuery knowledgeFileQuery) {
        QueryWrapper<KnowledgeFile> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(StringUtils.isNotBlank(knowledgeFileQuery.getKnowledgeBaseId()), KnowledgeFile::getKnowledgeBaseId, knowledgeFileQuery.getKnowledgeBaseId());
        wrapper.lambda().like(StringUtils.isNotBlank(knowledgeFileQuery.getFileName()), KnowledgeFile::getFileName, knowledgeFileQuery.getFileName());
        return wrapper;
    }

    /**
     * 按照条件查询知识库文件
     *
     * @param knowledgeFileQuery 查询条件
     * @return 条件结果信息
     */
    @Override
    @GetMapping
    public Result<List<KnowledgeFile>> list(KnowledgeFileQuery knowledgeFileQuery) {
        return Result.success(knowledgeFileService.list(getKnowledgeInfoQueryWrapper(knowledgeFileQuery)));
    }

    /**
     * 分页及条件查询知识库文件
     *
     * @param page               页码
     * @param size               分页大小
     * @param knowledgeFileQuery 查询条件
     * @return 分页结果信息
     */
    @Override
    @GetMapping("/{size}/{page}")
    public Result<PageResult<KnowledgeFile>> search(@PathVariable("size") Long size, @PathVariable("page") Long page, KnowledgeFileQuery knowledgeFileQuery) {
        if (page <= 0) {
            page = 1L;
        }
        if (size <= 0) {
            size = 10L;
        }
        Page<KnowledgeFile> pageParam = new Page<>(page, size);

        knowledgeFileService.page(pageParam, getKnowledgeInfoQueryWrapper(knowledgeFileQuery));
        PageResult<KnowledgeFile> pageResult = new PageResult<>(pageParam.getTotal(), pageParam.getSize(), pageParam.getCurrent(), pageParam.getRecords());

        return Result.success(pageResult);
    }

    /**
     * 根据ID获取知识库文件详情
     *
     * @param id 知识库文件ID
     * @return 知识库文件详细信息
     */
    @Override
    @GetMapping("/{id}")
    public Result<KnowledgeFile> detail(@PathVariable("id") String id) {
        return Result.success(knowledgeFileService.detailById(id));
    }

    /**
     * 增加知识库文件
     *
     * @param knowledgeFile 知识库文件增加实体
     * @return 增加后的知识库文件
     */
    @Override
    @PostMapping
    public Result<KnowledgeFile> add(@RequestBody KnowledgeFile knowledgeFile) {
        return Result.success(knowledgeFileService.saveKnowledgeInfo(knowledgeFile));
    }

    /**
     * 修改知识库文件
     *
     * @param id            知识库文件ID
     * @param knowledgeFile 知识库文件修改实体
     * @return 修改后的知识库文件
     */
    @Override
    @PutMapping("/{id}")
    public Result<KnowledgeFile> updateById(@PathVariable("id") String id, @RequestBody KnowledgeFile knowledgeFile) {
        knowledgeFile.setId(id);
        return Result.success(knowledgeFileService.updateKnowledgeInfoById(knowledgeFile));
    }


    /**
     * 删除知识库文件
     *
     * @param id 知识库文件ID
     * @return 删除结果
     */
    @Override
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(@PathVariable("id") String id) {
        knowledgeFileService.deleteById(id);
        return Result.success(true);
    }

    /**
     * 删除知识库文件
     *
     * @param idList 知识库文件ID数组
     * @return 删除结果
     */
    @Override
    @DeleteMapping("/batch-remove")
    public Result<Boolean> batchRemove(@RequestBody List<String> idList) {
        knowledgeFileService.removeByIds(idList);
        return Result.success(true);
    }
}
